////
//
/// \file   ./src/name_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/name_node.hpp>

#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/compound_name_node.hpp>
#include <inc/parser/label_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;

/// \brief  Parse a name node
/// \author Chandler Carruth
/// \date   2007.07.26
///
/// This builds a name parse node for use within expressions. It parses out 
/// either an explicit label for the name or hands off the parsing to some 
/// compound name construct.

bool
NameNode::parse( const iterator &first,
                 const iterator &last,
                 iterator &last_parsed )
{
  assert( get_text().empty() && !node_ptr
          && "This node was parsed more than once!" );

  //  Attempting to parse a label node.
  boost::scoped_ptr<ParseTreeNode> node( new LabelNode );
  if( node->parse( first, last, last_parsed ) )
  {
    set_text( node->get_text() );
    node_ptr.swap( node );
    return true;
  }

  //  Attempt to parse a compound name node.
  node.reset( new CompoundNameNode );
  if( node->parse( first, last, last_parsed ) )
  {
    set_text( node->get_text() );
    node_ptr.swap( node );
    return true;
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief  Convenience getter for a Label sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

LabelNode* NameNode::get_label() const
{
  return dynamic_cast<LabelNode*>( node_ptr.get() );
}

/// \brief  Convenience getter for a CompoundName sub-node
/// \author Chandler Carruth
/// \date   2008.05.16

CompoundNameNode* NameNode::get_compound_name() const
{
  return dynamic_cast<CompoundNameNode*>( node_ptr.get() );
}

} //  end parser namespace
} //  end inc namespace
